﻿namespace Okazuki.MVVM.PrismSupport.Interactivity
{
    using System;
    using System.Windows;
    using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

    /// <summary>
    /// Windowの最大化、最小化を行うアクション
    /// </summary>
    public class WindowStateChangeAction : DispatcherTriggerAction
    {
        private static readonly WindowStateChangeNotification NullObject = new WindowStateChangeNotification();

        public static readonly DependencyProperty WindowStateProperty =
            DependencyProperty.Register(
                "WindowState", 
                typeof(WindowState), 
                typeof(WindowStateChangeAction), 
                new PropertyMetadata(WindowState.Normal));

        /// <summary>
        /// 変更させるWindowState
        /// </summary>
        public WindowState WindowState
        {
            get { return (WindowState)GetValue(WindowStateProperty); }
            set { SetValue(WindowStateProperty, value); }
        }

        protected override void InvokeAction(InteractionRequestedEventArgs e)
        {
            var arg = e.Context as WindowStateChangeNotification ?? NullObject;
            Window.GetWindow(this.AssociatedObject).WindowState = arg.WindowState ?? this.WindowState;
            e.Callback();
        }
    }
}
